package com.henry.db.migrate.main;

import com.henry.db.migrate.factory.DbFactory;
import com.henry.db.migrate.model.TableModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class InitialTables {
    private static final Logger logger = Logger.getLogger(InitialTables.class);
    static final int tableNum = 9;
    public static final List<TableModel> tables;
    static File targetFile;

    static {
        tables = initTableModels();
        targetFile = FileUtils.getFile("resource/test", "init.sql");
    }

    public static void main(String[] args) {
        TableModel tableModel = tables.get(0);
        Connection srcConn = DbFactory.getConnection("src");
        try {
//            srcConn.setReadOnly(false);
            QueryRunner runner = new QueryRunner();
            srcConn.setAutoCommit(false);
//            Object[][] params = genParams(tableModel, 1);
//            runner.batch(srcConn, tableModel.getInsertSql(), params);
//            srcConn.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            DbFactory.rollback(srcConn);
        } finally {
            DbFactory.close(srcConn);
        }
    }

    private static boolean checkFile(){
        if(targetFile.exists()&&targetFile.isFile()&&targetFile.length()>10)
            return true;
        return false;
    }

    public static void initTables(){
        boolean flag = checkFile();
        if(flag){
            logger.info("数据库表已存在,不需要重新创建表...");
            return;
        }
        genInitSql(tables, targetFile);
        Connection srcConn = DbFactory.getConnection("src");
        Connection targetConn = DbFactory.getConnection("target");
        try {
            srcConn.setAutoCommit(false);
            targetConn.setAutoCommit(false);
            QueryRunner runner = new QueryRunner();
            for (TableModel tableModel : tables) {
                runner.update(srcConn, tableModel.getDropSql());
                runner.update(targetConn, tableModel.getDropSql());
                runner.update(srcConn, tableModel.getCreateSql());
                runner.update(targetConn, tableModel.getCreateSql());
            }
            srcConn.commit();
            targetConn.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            FileUtils.deleteQuietly(targetFile);
            DbFactory.rollback(srcConn);
            DbFactory.rollback(targetConn);
        } finally {
            DbFactory.close(srcConn);
            DbFactory.close(targetConn);
        }
    }

    private static List<TableModel> initTableModels() {
        List<TableModel> tableModels = new ArrayList<>();
        for (int i = 1; i <= tableNum; i++) {
            TableModel tableModel = new TableModel(i);
            tableModels.add(tableModel);
        }
        return tableModels;
    }

    private static void genInitSql(List<TableModel> tableModels, File file) {
        try {
            FileUtils.touch(file);
            FileUtils.write(file, "");
            for (TableModel tableModel : tableModels) {
                FileUtils.write(file, tableModel.getDropSql() + ";\r\n", true);
                FileUtils.write(file, tableModel.getCreateSql() + ";\r\n", true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
